package com.jinke.api.modules.third.easypost.model.response;

import cn.hutool.core.collection.CollectionUtil;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.jinke.api.common.api.CommonResult;
import lombok.Data;

import java.util.List;

@Data
public class GetPriceResponse {
    private Integer code;

    private List<GetPriceResponseDetail> rates;
    private Boolean success = true;
    private List<CreateOrderResponseDetail> messages;
    @Data
    public static class GetPriceResponseDetail {
        private String object;
        private String mode;
        private String service;
        private String carrier;
        private String rate;
        private String currency;
        @JsonProperty("retail_rate")
        private String retailRate;
        @JsonProperty("retail_currency")
        private String retailCurrency;
        @JsonProperty("list_rate")
        private String listRate;
        @JsonProperty("list_currency")
        private String listCurrency;
        @JsonProperty("billing_type")
        private String billingType;
        @JsonProperty("delivery_days")
        private int deliveryDays;
        @JsonProperty("delivery_date")
        private String deliveryDate;
        @JsonProperty("delivery_date_guaranteed")
        private boolean deliveryDateGuaranteed;
        @JsonProperty("est_delivery_days")
        private int estDeliveryDays;
        @JsonProperty("carrier_account_id")
        private String carrierAccountId;
    }

    public CommonResult toCommonResult() {
        // 判断是否成功
        CommonResult commonResult = CommonResult.success();
        if (!isSuccess()) {
            String msg = "";
            if(CollectionUtil.isNotEmpty(messages)){
                msg = messages.get(0).getMessage();
            }
            commonResult = CommonResult.failed(msg);
            success = false;
        }
        return commonResult;
    }
    @Data
    public static class CreateOrderResponseDetail {
        private String message;
    }

    public boolean isSuccess() {
        boolean result = true;
        // 判断是否成功
        if (CollectionUtil.isEmpty(rates)) {
            result = false;
        }
        return result;
    }

}
